package com.think.teaching;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bianxh on 2019/1/21.
 */
@Configuration
@EnableSwagger2
@MapperScan("com.think.teaching.mapper")
public class SwaggerApp {
	@Bean
	public Docket createRestApi() {
		ParameterBuilder ticketPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<>();
		ticketPar.name("access_token").description("AccessToken").modelRef(new ModelRef("string")).defaultValue("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpblR5cGUiOjQsInBheWxvYWQiOiI0aEN3REhtSUFmOEJqK1RWZTdHQlZVYUxPWTBySmlEeVd0Q21OaGl4SXNDQjJQbWtsZ1NRTFRJY2djeWFJVFlRQXpFb2QxbFNHZWdCVnZXK2V4QW5tNGJwaW0vUXU3aFBPaHZTV0w3amFCYz0iLCJ1c2VyX25hbWUiOiJ0ZXN0Iiwic2NvcGUiOlsiYWxsIl0sImV4cCI6MTY1NDIyNTE2MCwidXNlcklkIjo4NywiYXV0aG9yaXRpZXMiOlsiUk9MRV9OT1JNQUwiXSwianRpIjoiN2Y5YzlkMDMtMTU3My00M2JlLTk1ZDctNmE0ODRiNWY5ZWIxIiwiY2xpZW50X2lkIjoidGhpbmsifQ.AOrmzufzA9n8JBDA3wyI1LrRC_HU_yHovTR4GS3W6_g")
				.parameterType("query").required(false).build(); // header中的ticket参数非必填，传空也可以
		pars.add(ticketPar.build()); // 根据每个方法名也知道当前方法在设置什么参数
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				// 为当前包路径
				.apis(RequestHandlerSelectors.basePackage("com.think.teaching.controller")).paths(PathSelectors.any())
				.build().globalOperationParameters(pars);
	}

	// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				// 页面标题
				.title("虚拟仿真服务接口文档")
				// 创建人
				// 版本号
				.version("1.0")
				// 描述
				.description("API 描述").build();
	}

}
